﻿using System.IO;
using Microsoft.Practices.RecipeFramework;
using Microsoft.VisualStudio.Shell;
using VSSDKAssist.Assets.Actions.Base;
using VSSDKAssist.Assets.Actions.General;
using VSSDKAssist.Recipes.Common.Helpers;

namespace VSSDKAssist.Recipes.Common.Actions
{
    public class CreatePackageRegistryFragment : ActionBase
    {
        [Input(Required = true)]
        public RegistrationMethod RegistrationMethod { get; set; }

        [Input(Required = true)]
        public EnvDTE.Project Project { get; set; }

        [Output]
        public string FragmentContent { get; set; }

        protected override void OnExecute()
        {
            string outputPath = this.Project.ConfigurationManager.ActiveConfiguration.Properties.Item("OutputPath").Value.ToString();
            string buildPath = this.Project.Properties.Item("LocalPath").Value.ToString();
            string targetName = this.Project.Properties.Item("OutputFileName").Value.ToString();
            string assemblyFile = Path.Combine(buildPath, Path.Combine(outputPath, targetName));
            string wixFile = Path.GetTempFileName();

            if(File.Exists(assemblyFile))
            {
                using(ExecuteProcessAction executeProcessAction = new ExecuteProcessAction())
                {
                    executeProcessAction.Site = this.Site;
                    executeProcessAction.FileName =
                            Path.Combine(
                                Path.Combine(CommonHelper.GetVSSdkInstallPath(), Constants.VSSdkToolRelPath),
                                Constants.RegPkgTool);

                    executeProcessAction.Arguments = GetRegisterArguments(assemblyFile, wixFile);

                    executeProcessAction.Execute();
                }

                if(File.Exists(wixFile))
                {
                    string content = File.ReadAllText(wixFile);

                    if(this.RegistrationMethod == RegistrationMethod.CodeBase)
                    {
                        string oldContent = 
                            string.Format("[#File_{0}.dll]", this.Project.Properties.Item("AssemblyName").Value.ToString());

                        string newContent =
                            string.Format("[TARGETDIR]{0}.dll", this.Project.Properties.Item("AssemblyName").Value.ToString());

                        content = content.Replace(oldContent, newContent);
                    }

                    this.FragmentContent = content;

                    File.Delete(wixFile);
                }
            }
        }

        protected override void OnUndo()
        {
            //Do Nothing
        }

        private string GetRegisterArguments(string assemblyFile, string wixFile)
        {
            string arguments = string.Empty;

            switch(this.RegistrationMethod)
            {
                case RegistrationMethod.Assembly:
                    arguments += "/assembly ";
                    break;
                case RegistrationMethod.CodeBase:
                    arguments += "/codebase ";
                    break;
                case RegistrationMethod.Default:
                    break;
                default:
                    break;
            }

            if(assemblyFile.Contains(" "))
            {
                assemblyFile = string.Concat("\"", assemblyFile, "\"");
            }

            if(wixFile.Contains(" "))
            {
                wixFile = string.Concat("\"", wixFile, "\"");
            }

            arguments += string.Format("{0} /wixfile:{1}", assemblyFile, wixFile);

            return arguments;
        }
    }
}